<?php $this->JQuery()->javascriptCaptureStart() ?>
$j(document).ready(function() {
    $j('#resetpwd').click(function() {
        $j.get(
            '<?php echo $this->geturl('reset-password','user', null,'admin1', array('id'=> $this->user->_id))?>',
            function(data) {
                $j('#resetpwd').after('<span class="pwd-reseted">&nbsp;New password: '+data.pwd+'</span>');
            }
        );
        return false;
    });
});
<?php $this->JQuery()->javascriptCaptureEnd() ?>    

<?php $this->headStyle()->captureStart()?>
    #content td { padding:5px}
    #content table { border:1px #aaa solid; padding:5px; width:100%}
    #content th {font-weight:bold;}

<?php $this->headStyle()->captureEnd()?>
<?php
    $data  = Model_Message::all(array('fo.email' => $this->user->email));
    $count = Model_Message::all(array('fo.email' => $this->user->email))->count();
?>
<table class="actions">
    <tr>
    	<?php if ($this->user->status != Model_User::STATUS_BANNED){?>
        <td>
            <a href="<?php echo $this->geturl('ban','user', null, 'admin1', array('id'=> $this->user->_id))?>">Ban</a>
        </td>
        <?php }
		if ($this->user->status == Model_User::STATUS_DELETED || $this->user->status == Model_User::STATUS_BANNED) :?>
        <td>
            <a href="<?php echo $this->geturl('activate','user', null, 'admin1', array('id'=> $this->user->_id))?>">Activate</a>
        </td>
        <?php elseif (($this->user->status == Model_User::STATUS_ACTIVE) || (!isset($this->user->status)) || ($this->user->status == Model_User::STATUS_BANNED)) :?>
        <td>
            <a href="<?php echo $this->geturl('delete','user', null, 'admin1', array('id'=> $this->user->_id, 'bp'=> (empty($_GET['bp'])? "" : $_GET['bp'])))?>">Delete</a>
        </td>
        <?php endif;?>
        <td>
            <a href="<?php echo $this->user->url()?>" target="blank">Display in website</a>
        </td>
		<?php if (empty($this->user->cid)): ?>
		 <td>
            <a href="<?php echo $this->geturl('upgrade','user', null, 'admin1', array('id'=> $this->user->_id))?>" target="blank">Upgrade</a>
        </td>
		<?php else: ?>
		 <td>
            <a href="<?php echo $this->geturl('edit','user', null, 'admin1', array('id'=> $this->user->_id))?>" target="blank">Edit Info</a>
        </td>
		<td>
            <a href="<?php echo $this->geturl('reverse','user', null, 'admin1', array('id'=> $this->user->_id))?>" target="blank">Reverse</a>
        </td>
		<?php endif; ?>
        <td>
            <a href="#" id="resetpwd">Reset password</a>
        </td>        
    </tr>
</table>
<!--table id="user">
    
    <tr>
        <th>Username</th>
        <td><?php echo $this->user->username?></td>
    </tr>
    <tr>
        <th>email</th>
        <td><?php echo $this->user->email?></td>
    </tr>
    <tr>
        <th>firstname</th>
        <td><?php echo $this->user->firstname?></td>
    </tr>
    <tr>
        <th>lastname</th>
        <td><?php echo $this->user->lastname?></td>
    </tr>
    <tr>
        <th>phone</th>
        <td><?php echo $this->user->phone?></td>
    </tr>
    <tr>
        <th>profile picture</th>
        <td><?php echo $this->user->profile_picture?></td>
    </tr>

</table-->
<form method="post" enctype="multipart/form-data">
	<input type="hidden" name="id" value="<?php echo $_GET['id']?>">
	<table id="user_new">
		<tr>
	        <th>Status</th>
	        <td>
	            <?php
	            if ($this->user->status == Model_User::STATUS_ACTIVE || empty($this->user->status)) {
	                echo 'Active';
	            }
	            if ($this->user->status == Model_User::STATUS_BANNED) {
	                echo 'Banned';
	            }
	            if ($this->user->status == Model_User::STATUS_DELETED) {
	                echo 'Deleted';
	            }
	            ?>
	        </td>
	    </tr>
	    <!--tr><th colspan="2">User Profile</th></tr-->
		<tr>
	        <th>First Name</th>
	        <td><?php echo $this->user->firstname?></td>
	        <td><input type="text" name="firstname"></td>
	    </tr>
	    <tr>
	        <th>Last Name</th>
	        <td><?php echo $this->user->lastname?></td>
	        <td><input type="text" name="lastname"></td>
	    </tr>
	    <tr>
	        <th>Display Name</th>
	        <td><?php echo $this->user->username?></td>
	        <td><input type="text" name="display"> <?php echo !empty($this->existingdisplay) ? $this->existingdisplay." duplicate" : ""?></td>
	    </tr>
	    <tr>
	        <th>Phone</th>
	        <td><?php echo $this->user->phone?></td>
	        <td><input type="text" name="phone"> </td>
	    </tr>
	    <tr>
	        <th>Profile Picture</th>
	        <td><?php echo $this->user->profile_picture?></td>
	        <td><input type="file" name="pic"> </td>
	    </tr>
	    <!--tr><th colspan="2">Account Setting</th></tr-->
	    <tr>
	        <th>Email</th>
	        <td><?php echo $this->user->email?></td>
	        <td><input type="text" name="email">  <?php echo !empty($this->existingemail) ? $this->existingemail." duplicate" : ""?></td>
	    </tr>
	    <tr>
	        <th>Language</th>
	        <td><?php echo $this->user->la?></td>
	        <td>
				<select name="language">
					<option>Please Select</option>
					<option value="en">English</option>
					<option value="tc">Chinese</option>
				</select>
			</td>
	    </tr>
	    <tr>
	        <th valign="top">Notification</th>
	        <td valign="top"><?php echo $this->user->nl?></td>
	        <td valign="top">
				<select name="notif">
					<option>Please Select</option>
					<option value="1">Yes</option>
					<option value="0">No</option>
				</select>
			</td>
	    </tr>
	    <tr>
	        <th valign="top">About Us</th>
	        <td valign="top"><?php echo $this->user->au?></td>
	        <td valign="top">
				<textarea name="about_us"></textarea>
				<br />
				<input type="submit" name="update" value="Update">
			</td>
	    </tr>
	</table>
	
	<table>	
		<tr>
	        <th valign="top"><b>Listing Mass Updater</b></th>
	        <td valign="top"> 
			</td>
	    </tr>
		<tr>
	        <th valign="top" width="250">Listing Location</th>
	        <td valign="top">
	        	<?php
	        	// --> District
		        $locs = Model_Location::all()->sort(array('path' => 1));
		        $parent = '';
		        foreach($locs as $k => $loc) {
		
		            if (count($loc->path) == 1) {
		                $parent = $loc->name;
		                $locations[$loc->name] = array();
		            } else {
		
		                //$parent = (string) $loc->path[0];
		                $tmp    = !empty($locations[(string) $parent]) ? $locations[(string) $parent] : array();
		
		                $tmp[$k]         = (string) $loc->name;
		                //echo '<pre>';
		                //print_r($tmp);
		                $locations[(string) $parent] = $tmp;
		            }
		        }
	        	?>
				<select name="location">
					<option value=""><?php echo $this->t('Please Select')?></option>
				<?php foreach($locations as $k => $locs):?>
					<optgroup label="<?php echo $k?>">
					<?php foreach($locs as $j => $loc):?>
						<option value="<?php echo $j?>"><?php echo $loc?></option>
					<?php endforeach;?>
					</optgroup>
				<?php endforeach;?>
				</select>
			</td>
	    </tr>
		<tr>
	        <th valign="top">Listing Address</th>
	        <td valign="top">
				<textarea name="address"></textarea>
				<br />
				<input type="submit" name="save" value="Save">
			</td>
	    </tr>
	</table>
</form>


<?php if ($count != 0):?>
<table border="1" id="messages">
    <tr>
        <td colspan="4">&nbsp;</td>
    </tr>
    <tr>
        <th colspan="4">Messages (<?php echo $count?>)</th>
    </tr>
    <tr>
        <th>Subject</th>
        <th>From</th>
        <th>To</th>
        <th>Creation date</th>
    </tr>
    <?php foreach ($data as $item):?>
    <tr>
        <td>
            <a href="<?php echo $this->geturl('display', 'message', null, 'admin1', array('id'=> $item->_id))?>" target="_blank">
                <?php echo $item->su; ?>
            </a>
        </td>
        <td>
            <?php
            if (is_array($item->fo)) {
                echo $item->fo['email'];
            }
            else {
                echo $item->fo;
            }
            ?>
        </td>
         <td>
            <?php echo $item->to; ?>
        </td>
        <td>
            <?php if (!empty($item->created_at)) {echo date(MEDIUM_DATE_FORMAT, strtotime($item->created_at));}?>
        </td>
    </tr>
    <?php endforeach; ?>
</table>
<?php endif;?>

<?php $data  = Model_Listing::all(array('us' => new MongoId($this->user->_id)))->sort(array('p_dt' => -1)); ?>
<?php $count = Model_Listing::all(array('us' => new MongoId($this->user->_id)))->count(); ?>
<?php if ($count != 0):?>
<table border="1" id="listings">
    <tr>
        <td colspan="3">&nbsp;</td>
    </tr>
    <tr>
        <th colspan="3">Listing (<?php echo $count?>)</th>
    </tr>
    <tr>
        <th>Title</th>
        <th>Creation Date</th>
        <th>Status</th>
    </tr>
    <?php foreach ($data as $item):?>
    <tr>
        <td>
            <a href="<?php echo $this->geturl('display','listing', null, 'admin1', array('id'=> $item->_id))?>" target="_blank">
                <?php echo htmlentities($item->title, ENT_COMPAT, 'utf-8'); ?>
            </a>
        </td>
        <td>
            <?php echo date (MEDIUM_DATE_FORMAT, $item->posted_at)?>
        </td>
        <td>
            <?php
            if ($item->status == Model_Listing::STATUS_PUBLISHED) {
                echo 'Published';
            }
            if ($item->status == Model_Listing::STATUS_STARTED) {
                echo 'Started';
            }
            if ($item->status == Model_Listing::STATUS_DRAFT) {
                echo 'Draft';
            }
            if ($item->status == Model_Listing::STATUS_DELETED) {
                echo 'Deleted';
            }
            if ($item->status == Model_Listing::STATUS_BANNED) {
                echo 'Banned';
            }
            if ($item->status == Model_Listing::STATUS_MARK_SOLD) {
                echo 'Marked as Sold';
            }
            if ($item->status == Model_Listing::STATUS_PUBLISHED_DELETED) {
                echo 'PUBLISHED_DELETED';
            }
            ?>
        </td>
    </tr>
    <?php endforeach; ?>
</table>
<?php endif;?>
